home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / mpeg / decoders.h < prev    next >
C/C++ Source or Header  |  1994-08-01  |  16KB  |  480 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. /*
  22.  * decoders.h
  23.  *
  24.  * This file contains the declarations of structures required for Huffman
  25.  * decoding
  26.  *
  27.  */
  28.  
  29. #ifndef DECODERS_H_
  30. #define DECODERS_H_
  31.  
  32. /* Include util.h for bit i/o parsing macros. */
  33.  
  34. #include "util.h"
  35.  
  36. /* Code for unbound values in decoding tables */
  37. #define ERROR -1
  38. #define DCT_ERROR 63
  39.  
  40. #define MACRO_BLOCK_STUFFING 34
  41. #define MACRO_BLOCK_ESCAPE 35
  42.  
  43. /* Two types of DCT Coefficients */
  44. #define DCT_COEFF_FIRST 0
  45. #define DCT_COEFF_NEXT 1
  46.  
  47. /* Special values for DCT Coefficients */
  48. #define END_OF_BLOCK 62
  49. #define ESCAPE 61
  50.  
  51. /* Structure for an entry in the decoding table of 
  52.  * macroblock_address_increment */
  53. typedef struct {
  54.   unsigned int value;       /* value for macroblock_address_increment */
  55.   int num_bits;             /* length of the Huffman code */
  56. } mb_addr_inc_entry;
  57.  
  58. /* Decoding table for macroblock_address_increment */
  59. extern mb_addr_inc_entry mb_addr_inc[2048];
  60.  
  61.  
  62. /* Structure for an entry in the decoding table of macroblock_type */
  63. typedef struct {
  64.   unsigned int mb_quant;              /* macroblock_quant */
  65.   unsigned int mb_motion_forward;     /* macroblock_motion_forward */
  66.   unsigned int mb_motion_backward;    /* macroblock_motion_backward */
  67.   unsigned int mb_pattern;            /* macroblock_pattern */
  68.   unsigned int mb_intra;              /* macroblock_intra */
  69.   int num_bits;                       /* length of the Huffman code */
  70. } mb_type_entry;
  71.  
  72. /* Decoding table for macroblock_type in predictive-coded pictures */
  73. extern mb_type_entry mb_type_P[64];
  74.  
  75. /* Decoding table for macroblock_type in bidirectionally-coded pictures */
  76. extern mb_type_entry mb_type_B[64];
  77.  
  78.  
  79. /* Structures for an entry in the decoding table of coded_block_pattern */
  80. typedef struct {
  81.   unsigned int cbp;            /* coded_block_pattern */
  82.   int num_bits;                /* length of the Huffman code */
  83. } coded_block_pattern_entry;
  84.  
  85. /* External declaration of coded block pattern table. */
  86.  
  87. extern coded_block_pattern_entry coded_block_pattern[512];
  88.  
  89.  
  90.  
  91. /* Structure for an entry in the decoding table of motion vectors */
  92. typedef struct {
  93.   int code;              /* value for motion_horizontal_forward_code,
  94.               * motion_vertical_forward_code, 
  95.               * motion_horizontal_backward_code, or
  96.               * motion_vertical_backward_code.
  97.               */
  98.   int num_bits;          /* length of the Huffman code */
  99. } motion_vectors_entry;
  100.  
  101.  
  102. /* Decoding table for motion vectors */
  103. extern motion_vectors_entry motion_vectors[2048];
  104.  
  105.  
  106. /* Structure for an entry in the decoding table of dct_dc_size */
  107. typedef struct {
  108.   unsigned int value;    /* value of dct_dc_size (luminance or chrominance) */
  109.   int num_bits;          /* length of the Huffman code */
  110. } dct_dc_size_entry;
  111.  
  112. /* External declaration of dct dc size lumiance table. */
  113.  
  114. extern dct_dc_size_entry dct_dc_size_luminance[128];
  115.  
  116. /* External declaration of dct dc size chrom table. */
  117.  
  118. extern dct_dc_size_entry dct_dc_size_chrominance[256];
  119.  
  120.  
  121. /* DCT coeff tables. */
  122.  
  123. #define RUN_MASK 0xfc00
  124. #define LEVEL_MASK 0x03f0
  125. #define NUM_MASK 0x000f
  126. #define RUN_SHIFT 10
  127. #define LEVEL_SHIFT 4
  128.  
  129. /* External declaration of dct coeff tables. */
  130.  
  131. extern unsigned short int dct_coeff_tbl_0[256];
  132. extern unsigned short int dct_coeff_tbl_1[16];
  133. extern unsigned short int dct_coeff_tbl_2[4];
  134. extern unsigned short int dct_coeff_tbl_3[4];
  135. extern unsigned short int dct_coeff_next[256];
  136. extern unsigned short int dct_coeff_first[256];
  137.  
  138. #define DecodeDCTDCSizeLum(macro_val)                    \
  139. {                                                    \
  140.   unsigned int index;                                \
  141.                                                      \
  142.   show_bits7(index);                              \
  143.                                                      \
  144.   macro_val = dct_dc_size_luminance[index].value;       \
  145.                                                      \
  146.   flush_bits(dct_dc_size_luminance[index].num_bits); \
  147. }
  148.  
  149. #define DecodeDCTDCSizeChrom(macro_val)                      \
  150. {                                                        \
  151.   unsigned int index;                                    \
  152.                                                          \
  153.   show_bits8(index);                                  \
  154.                                                          \
  155.   macro_val = dct_dc_size_chrominance[index].value;         \
  156.                                                          \
  157.   flush_bits(dct_dc_size_chrominance[index].num_bits);   \
  158. }
  159.  
  160. #define DecodeDCTCoeff(dct_coeff_tbl, run, level)            \
  161. {                                    \
  162.   unsigned int temp, index;                        \
  163.   unsigned int value, next32bits, flushed;                \
  164.                                     \
  165.   /*                                    \
  166.    * Grab the next 32 bits and use it to improve performance of        \
  167.    * getting the bits to parse. Thus, calls are translated as:        \
  168.    *                                    \
  169.    *    show_bitsX  <-->   next32bits >> (32-X)                \
  170.    *    get_bitsX   <-->   val = next32bits >> (32-flushed-X);        \
  171.    *               flushed += X;                \
  172.    *               next32bits &= bitMask[flushed];        \
  173.    *    flush_bitsX <-->   flushed += X;                \
  174.    *               next32bits &= bitMask[flushed];        \
  175.    *                                    \
  176.    * I've streamlined the code a lot, so that we don't have to mask    \
  177.    * out the low order bits and a few of the extra adds are removed.    \
  178.    */                                    \
  179.   show_bits32(next32bits);                        \
  180.                                     \
  181.   /* show_bits8(index); */                        \
  182.   index = next32bits >> 24;                        \
  183.                                     \
  184.   if (index > 3) {                            \
  185.     value = dct_coeff_tbl[index];                    \
  186.     run = value >> RUN_SHIFT;                        \
  187.     if (run != END_OF_BLOCK) {                        \
  188.       /* num_bits = (value & NUM_MASK) + 1; */                \
  189.       /* flush_bits(num_bits); */                    \
  190.       if (run != ESCAPE) {                        \
  191.      /* get_bits1(value); */                    \
  192.      /* if (value) level = -level; */                \
  193.      flushed = (value & NUM_MASK) + 2;                \
  194.          level = (value & LEVEL_MASK) >> LEVEL_SHIFT;            \
  195.      value = next32bits >> (32-flushed);                \
  196.      value &= 0x1;                            \
  197.      if (value) level = -level;                    \
  198.      /* next32bits &= ((~0) >> flushed);  last op before update */    \
  199.        }                                \
  200.        else {    /* run == ESCAPE */                    \
  201.      /* Get the next six into run, and next 8 into temp */        \
  202.          /* get_bits14(temp); */                    \
  203.      flushed = (value & NUM_MASK) + 1;                \
  204.      temp = next32bits >> (18-flushed);                \
  205.      /* Normally, we'd ad 14 to flushed, but I've saved a few    \
  206.       * instr by moving the add below */                \
  207.      temp &= 0x3fff;                        \
  208.      run = temp >> 8;                        \
  209.      temp &= 0xff;                            \
  210.      if (temp == 0) {                        \
  211.             /* get_bits8(level); */                    \
  212.         level = next32bits >> (10-flushed);                \
  213.         level &= 0xff;                        \
  214.         flushed += 22;                        \
  215.          assert(level >= 128);                    \
  216.      } else if (temp != 128) {                    \
  217.         /* Grab sign bit */                        \
  218.         flushed += 14;                        \
  219.         level = ((int) (temp << 24)) >> 24;                \
  220.      } else {                            \
  221.             /* get_bits8(level); */                    \
  222.         level = next32bits >> (10-flushed);                \
  223.         level &= 0xff;                        \
  224.         flushed += 22;                        \
  225.         level = level - 256;                    \
  226.         assert(level <= -128 && level >= -255);            \
  227.      }                                \
  228.        }                                \
  229.        /* Update bitstream... */                    \
  230.        flush_bits(flushed);                        \
  231.        assert (flushed <= 32);                        \
  232.     }                                    \
  233.   }                                    \
  234.   else {                                \
  235.     if (index == 2) {                             \
  236.       /* show_bits10(index); */                        \
  237.       index = next32bits >> 22;                        \
  238.       value = dct_coeff_tbl_2[index & 3];                \
  239.     }                                    \
  240.     else if (index == 3) {                         \
  241.       /* show_bits10(index); */                        \
  242.       index = next32bits >> 22;                        \
  243.       value = dct_coeff_tbl_3[index & 3];                \
  244.     }                                    \
  245.     else if (index) {    /* index == 1 */                \
  246.       /* show_bits12(index); */                        \
  247.       index = next32bits >> 20;                        \
  248.       value = dct_coeff_tbl_1[index & 15];                \
  249.     }                                    \
  250.     else {   /* index == 0 */                        \
  251.       /* show_bits16(index); */                        \
  252.       index = next32bits >> 16;                        \
  253.       value = dct_coeff_tbl_0[index & 255];                \
  254.     }                                    \
  255.     run = value >> RUN_SHIFT;                        \
  256.     level = (value & LEVEL_MASK) >> LEVEL_SHIFT;            \
  257.                                     \
  258.     /*                                    \
  259.      * Fold these operations together to make it fast...        \
  260.      */                                    \
  261.     /* num_bits = (value & NUM_MASK) + 1; */                \
  262.     /* flush_bits(num_bits); */                        \
  263.     /* get_bits1(value); */                        \
  264.     /* if (value) level = -level; */                    \
  265.                                     \
  266.     flushed = (value & NUM_MASK) + 2;                    \
  267.     value = next32bits >> (32-flushed);                    \
  268.     value &= 0x1;                            \
  269.     if (value) level = -level;                        \
  270.                                     \
  271.     /* Update bitstream ... */                        \
  272.     flush_bits(flushed);                        \
  273.     assert (flushed <= 32);                        \
  274.   }                                    \
  275. }
  276.  
  277. #define DecodeDCTCoeffFirst(runval, levelval)         \
  278. {                                                     \
  279.   DecodeDCTCoeff(dct_coeff_first, runval, levelval);  \
  280. }          
  281.  
  282. #define DecodeDCTCoeffNext(runval, levelval)          \
  283. {                                                     \
  284.   DecodeDCTCoeff(dct_coeff_next, runval, levelval);   \
  285. }
  286.  
  287. /*
  288.  *--------------------------------------------------------------
  289.  *
  290.  * DecodeMBAddrInc --
  291.  *
  292.  *      Huffman Decoder for macro_block_address_increment; the location
  293.  *      in which the result will be placed is being passed as argument.
  294.  *      The decoded value is obtained by doing a table lookup on
  295.  *      mb_addr_inc.
  296.  *
  297.  * Results:
  298.  *      The decoded value for macro_block_address_increment or ERROR
  299.  *      for unbound values will be placed in the location specified.
  300.  *
  301.  * Side effects:
  302.  *      Bit stream is irreversibly parsed.
  303.  *
  304.  *--------------------------------------------------------------
  305.  */
  306. #define DecodeMBAddrInc(val)                \
  307. {                            \
  308.     unsigned int index;                    \
  309.     show_bits11(index);                    \
  310.     val = mb_addr_inc[index].value;            \
  311.     flush_bits(mb_addr_inc[index].num_bits);        \
  312. }
  313.  
  314. /*
  315.  *--------------------------------------------------------------
  316.  *
  317.  * DecodeMotionVectors --
  318.  *
  319.  *      Huffman Decoder for the various motion vectors, including
  320.  *      motion_horizontal_forward_code, motion_vertical_forward_code,
  321.  *      motion_horizontal_backward_code, motion_vertical_backward_code.
  322.  *      Location where the decoded result will be placed is being passed
  323.  *      as argument. The decoded values are obtained by doing a table
  324.  *      lookup on motion_vectors.
  325.  *
  326.  * Results:
  327.  *      The decoded value for the motion vector or ERROR for unbound
  328.  *      values will be placed in the location specified.
  329.  *
  330.  * Side effects:
  331.  *      Bit stream is irreversibly parsed.
  332.  *
  333.  *--------------------------------------------------------------
  334.  */
  335.  
  336. #define DecodeMotionVectors(value)            \
  337. {                            \
  338.   unsigned int index;                    \
  339.   show_bits11(index);                    \
  340.   value = motion_vectors[index].code;            \
  341.   flush_bits(motion_vectors[index].num_bits);        \
  342. }
  343. /*
  344.  *--------------------------------------------------------------
  345.  *
  346.  * DecodeMBTypeB --
  347.  *
  348.  *      Huffman Decoder for macro_block_type in bidirectionally-coded
  349.  *      pictures;locations in which the decoded results: macroblock_quant,
  350.  *      macroblock_motion_forward, macro_block_motion_backward,
  351.  *      macroblock_pattern, macro_block_intra, will be placed are
  352.  *      being passed as argument. The decoded values are obtained by
  353.  *      doing a table lookup on mb_type_B.
  354.  *
  355.  * Results:
  356.  *      The various decoded values for macro_block_type in
  357.  *      bidirectionally-coded pictures or ERROR for unbound values will
  358.  *      be placed in the locations specified.
  359.  *
  360.  * Side effects:
  361.  *      Bit stream is irreversibly parsed.
  362.  *
  363.  *--------------------------------------------------------------
  364.  */
  365. #define DecodeMBTypeB(quant, motion_fwd, motion_bwd, pat, intra)    \
  366. {                                    \
  367.   unsigned int index;                            \
  368.                                     \
  369.   show_bits6(index);                            \
  370.                                     \
  371.   quant = mb_type_B[index].mb_quant;                    \
  372.   motion_fwd = mb_type_B[index].mb_motion_forward;            \
  373.   motion_bwd = mb_type_B[index].mb_motion_backward;            \
  374.   pat = mb_type_B[index].mb_pattern;                    \
  375.   intra = mb_type_B[index].mb_intra;                    \
  376.   flush_bits(mb_type_B[index].num_bits);                \
  377. }
  378. /*
  379.  *--------------------------------------------------------------
  380.  *
  381.  * DecodeMBTypeI --
  382.  *
  383.  *      Huffman Decoder for macro_block_type in intra-coded pictures;
  384.  *      locations in which the decoded results: macroblock_quant,
  385.  *      macroblock_motion_forward, macro_block_motion_backward,
  386.  *      macroblock_pattern, macro_block_intra, will be placed are
  387.  *      being passed as argument.
  388.  *
  389.  * Results:
  390.  *      The various decoded values for macro_block_type in intra-coded
  391.  *      pictures or ERROR for unbound values will be placed in the
  392.  *      locations specified.
  393.  *
  394.  * Side effects:
  395.  *      Bit stream is irreversibly parsed.
  396.  *
  397.  *--------------------------------------------------------------
  398.  */
  399. #define DecodeMBTypeI(quant, motion_fwd, motion_bwd, pat, intra)    \
  400. {                                    \
  401.   unsigned int index;                            \
  402.   static int quantTbl[4] = {ERROR, 1, 0, 0};                \
  403.                                     \
  404.   show_bits2(index);                            \
  405.                                     \
  406.   motion_fwd = 0;                            \
  407.   motion_bwd = 0;                            \
  408.   pat = 0;                                \
  409.   intra = 1;                                \
  410.   quant = quantTbl[index];                        \
  411.   if (index) {                                \
  412.     flush_bits (1 + quant);                        \
  413.   }                                    \
  414. }
  415. /*
  416.  *--------------------------------------------------------------
  417.  *
  418.  * DecodeMBTypeP --
  419.  *
  420.  *      Huffman Decoder for macro_block_type in predictive-coded pictures;
  421.  *      locations in which the decoded results: macroblock_quant,
  422.  *      macroblock_motion_forward, macro_block_motion_backward,
  423.  *      macroblock_pattern, macro_block_intra, will be placed are
  424.  *      being passed as argument. The decoded values are obtained by
  425.  *      doing a table lookup on mb_type_P.
  426.  *
  427.  * Results:
  428.  *      The various decoded values for macro_block_type in
  429.  *      predictive-coded pictures or ERROR for unbound values will be
  430.  *      placed in the locations specified.
  431.  *
  432.  * Side effects:
  433.  *      Bit stream is irreversibly parsed.
  434.  *
  435.  *--------------------------------------------------------------
  436.  */
  437. #define DecodeMBTypeP(quant, motion_fwd, motion_bwd, pat, intra)    \
  438. {                                    \
  439.   unsigned int index;                            \
  440.                                     \
  441.   show_bits6(index);                            \
  442.                                     \
  443.   quant = mb_type_P[index].mb_quant;                    \
  444.   motion_fwd = mb_type_P[index].mb_motion_forward;            \
  445.   motion_bwd = mb_type_P[index].mb_motion_backward;            \
  446.   pat = mb_type_P[index].mb_pattern;                    \
  447.   intra = mb_type_P[index].mb_intra;                    \
  448.                                     \
  449.   flush_bits(mb_type_P[index].num_bits);                \
  450. }
  451. /*
  452.  *--------------------------------------------------------------
  453.  *
  454.  * DecodeCBP --
  455.  *
  456.  *      Huffman Decoder for coded_block_pattern; location in which the
  457.  *      decoded result will be placed is being passed as argument. The
  458.  *      decoded values are obtained by doing a table lookup on
  459.  *      coded_block_pattern.
  460.  *
  461.  * Results:
  462.  *      The decoded value for coded_block_pattern or ERROR for unbound
  463.  *      values will be placed in the location specified.
  464.  *
  465.  * Side effects:
  466.  *      Bit stream is irreversibly parsed.
  467.  *
  468.  *--------------------------------------------------------------
  469.  */
  470. #define DecodeCBP(coded_bp)                        \
  471. {                                    \
  472.   unsigned int index;                            \
  473.                                     \
  474.   show_bits9(index);                            \
  475.   coded_bp = coded_block_pattern[index].cbp;                \
  476.   flush_bits(coded_block_pattern[index].num_bits);            \
  477. }
  478.  
  479. #endif
  480.